<?php 

class pictureController extends appController
{
	private $jssdk;
    public function _initialize()
    {
//      模拟登录
		
        
		if(!$_SESSION['wxid'])
		{
		    
		    $_SESSION['wxid'] = 1;
		}
		
		$this->assign("wxid", $_SESSION['wxid']);
		
// 		if($_SESSION['wxid']!=1)
// 		{
// 		    echo $_SESSION['wxid'];
// 		}
		
//      网页授权
//         $this->check_member();
       
        
        
//      构建系统配置信息
        $config[c] = array('site_name' => "报料-".Model('app')->get_cfg('site_name'));
        $this->assign_global($config);
        
//      构建jssdk配置信息
        $jsdk = Model('suipai')->get_jsdk($_GET['url']);
        $this->jssdk = $jsdk;
        $this->assign("jsdk", $jsdk);
        
    }
    
    
	public function clear()
	{
		unset($_SESSION['wxid']);
	}
	
	public function set()
	{
	    $_SESSION['wxid'] = $_GET['wxid'];
	}
// 	首页

	public function share()
	{
	    $match = Model('suipai')->get_parent($_GET['wxid']);
	    
	    $this->assign("match", $match);
	    
	    $this->display("share.html");
	}
	
	public function index()
	{
		
		
	    if($_GET['wxid']>1)
	    {
	        
	        header("Location:http://bgy2.lly360.com//ws/index.php?g=mobile&m=picture&a=share&wxid=".$_GET['wxid']);
	         
	        $match = Model('suipai')->get_parent($_GET['wxid']);
	         
	        $this->assign("parent", $match);
	    }
	    
		$this->display("index.html");
	}
	
	
// 	下载页
	public function match()
	{
	    
	    $match = Model('suipai')->get_match();
	    
	    $this->assign("match", $match);
	    
// 	    var_dump($match);
	    
		$this->display("3match1.html");
	}
	
	

	

	
// 	作品上传
	public function upload()
	{
// 	    if($_SESSION['wxid']>1)
// 	    {
// 	        header("Location:http://bgy2.lly360.com//ws/index.php?g=mobile&m=picture&a=match");
// 	    }
	    
		$this->display("2upload.html");
	}

	
	
	//作品提交
	public function send_set()
	{
		$res = array('error' => 1, 'content' => '');
		$img = yf_empty($_REQUEST['pic'], "请上传你的照片");
		$img1 = yf_empty($_REQUEST['pic1'], "请上传ta的照片");
		$info['ranks'] = rand(1,5);
		$info['pic'] = $img;
		$info['pic1'] = $img1;
		$info['created'] = yf_time();
		$id = Model('suipai')->insert_record($info,"picture");
		if($id)
		{
		    $_SESSION['wxid'] = $id;
			
			$res = array('error' => 0, 'content' => '提交成功');
		}
		else 
		{
			$res['content'] = "系统繁忙";
		}
		
		$this->showMsg($res);
	}
	
// 	图片上传处理
	public function upload_img()
	{
// 		$json = file_get_contents($url);
// 		$arr = json_decode($json,true);
		
		
// 		$open=fopen("log.txt","a" );
// 		fwrite($open,$url."\r\n");
// 		fclose($open);
		$res = array('error' => 1, 'content' => '');
		$media_id = yf_empty($_POST['img'], "error");
		$token = Model('suipai')->getAccessToken2();
		$url="http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=".$token."&media_id=".$media_id;
		$open=fopen("log.txt","a" );
				fwrite($open,$url."\r\n");
				fclose($open);
		$img = yf_time().".jpg";
		$img = $this->GrabImage($url, $img);
		$temp = str_replace(ROOT_PATH, "/", $img);
		if($_POST['area']=="img-area")
		{
		    $html = "<input type=\"hidden\" name=\"pic\" value=\"$temp\">";
		}
		else
		{
		    $html = "<input type=\"hidden\" name=\"pic1\" value=\"$temp\">";
		}
		
		$url = "<img src=\"$temp\"   class=\"img-responsive\"    >";
		$res = array('error' => 0, 'content' => $html , 'url' =>$url ,'img'=>'http://bgy2.lly360.com/ws/'.$temp);
		$this->showMsg($res);
	}
	
	
// 	图片抓取
	public function GrabImage($url, $filename = "") {
		if($url == ""):return false;endif;
		if($filename == "") {
			$ext = strrchr($url, ".");
			if($ext != ".gif" && $ext != ".jpg"):return false;endif;
			$filename = date("YmdHis").$ext;
		}
		ob_start();
		readfile($url);
		$img = ob_get_contents();
		ob_end_clean();
		$size = strlen($img);

		$file = 'Uploads/img/'.date('Ym');
		if(!is_dir($file))
		{
			mkdir($file);
		}
		$file = $file."/".$filename;
		$fp2 = @fopen($file, "a");
		fwrite($fp2, $img);
		fclose($fp2);
		return $file;
	}
	
	public function get_wx_info(){
		print_r(json_encode($this->jssdk));
	}
	
	public function get_match(){
		$match = Model('suipai')->get_match();
		$result = array("jssdk"=>$this->jssdk,"match"=>$match);
		print_r(json_encode($result));
	}
	
	public function get_share(){
		$match = Model('suipai')->get_parent($_GET['wxid']);
		$result = array("jssdk"=>$this->jssdk,"match"=>$match);
		print_r(json_encode($result));
	}
	
	
}

?>